package com.ruoyi.duty.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.duty.domain.bo.DutyYfjyBo;
import com.ruoyi.duty.domain.vo.DutyYfjyVo;
import com.ruoyi.duty.service.IDutyYfjyService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 预防教育
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/duty/yfjy")
public class DutyYfjyController extends BaseController {

    private final IDutyYfjyService iDutyYfjyService;

    /**
     * 查询预防教育列表
     */
    @GetMapping("/list")
    public TableDataInfo<DutyYfjyVo> list(DutyYfjyBo bo, PageQuery pageQuery) {
        return iDutyYfjyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出预防教育列表
     */
    @Log(title = "预防教育", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DutyYfjyBo bo, HttpServletResponse response) {
        List<DutyYfjyVo> list = iDutyYfjyService.queryList(bo);
        ExcelUtil.exportExcel(list, "预防教育", DutyYfjyVo.class, response);
    }

    /**
     * 获取预防教育详细信息
     *
     * @param yfjyId 主键
     */
    @GetMapping("/{yfjyId}")
    public R<DutyYfjyVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long yfjyId) {
        return R.ok(iDutyYfjyService.queryById(yfjyId));
    }

    /**
     * 新增预防教育
     */
    @SaCheckPermission("duty:yfjy:add")
    @Log(title = "预防教育", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DutyYfjyBo bo) {
        return toAjax(iDutyYfjyService.insertByBo(bo));
    }

    /**
     * 修改预防教育
     */
    @SaCheckPermission("duty:yfjy:edit")
    @Log(title = "预防教育", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DutyYfjyBo bo) {
        return toAjax(iDutyYfjyService.updateByBo(bo));
    }

    /**
     * 删除预防教育
     *
     * @param yfjyIds 主键串
     */
    @SaCheckPermission("duty:yfjy:remove")
    @Log(title = "预防教育", businessType = BusinessType.DELETE)
    @DeleteMapping("/{yfjyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] yfjyIds) {
        return toAjax(iDutyYfjyService.deleteWithValidByIds(Arrays.asList(yfjyIds), true));
    }
}
